package com.example.demo.controller;
        import com.example.demo.entity.*;
        import com.example.demo.service.*;
        import com.example.demo.util.MyDateUtils;
        import org.springframework.beans.factory.annotation.Autowired;
        import org.springframework.http.HttpRequest;
        import org.springframework.stereotype.Controller;
        import org.springframework.ui.Model;
        import org.springframework.web.bind.annotation.*;
        import javax.servlet.http.HttpServletRequest;
        import javax.servlet.http.HttpSession;
        import java.util.List;
        import java.util.Date;
@Controller
@CrossOrigin
public class ClassroomrentalController {
    @Autowired
    private ClassroomrentalService classroomrentalService;
    @Autowired
    private ClassroomtypeService classroomtypeService;
    @Autowired
    private ClassroomService classroomService;
    @Autowired
    private BuildingService buildingService;
    @Autowired
    private LeasetimeService leasetimeService;
    @Autowired
    private UserService userService;
    //显示教室租赁列表
    @RequestMapping("classroomrental")
    public String leasetimelist(Model model) {
        List<ClassroomRentalEntity> classroomRentalEntities = classroomrentalService.queryClassroomrentalAll();
        model.addAttribute("classroomRentalList",classroomRentalEntities);
        return "classroomrental-list";
    }
    //添加教室租赁的html
    @RequestMapping("addClassroomrentalHtml")
    public String  addBuildingHtml(HttpSession session,Model model, HttpServletRequest request)
    {
        UserEntity user=(UserEntity)session.getAttribute("userEntity");
        model.addAttribute("loginuser",user);
        //展示所有楼栋-----》
        List<BuildingEntity> buildingEntities = buildingService.queryBuildingAll();
        model.addAttribute("buildingList",buildingEntities);
        //展示所有租赁时长
        List<LeaseTimeEntity> leaseTimeEntities = leasetimeService.queryLeasetimeAll();
        model.addAttribute("leaseTimeList",leaseTimeEntities);
        return "classroomrental-add";
    }

    //根据选择的教室类型id查询有此楼下有多少空闲教室 0
    @RequestMapping("selectClassTypeByBuilding")
    @ResponseBody
    public  List<ClassroomTypeEntity>  selectClassTypeByBuilding(@RequestParam( "bid") int bid,HttpServletRequest request, Model model) {
        List<ClassroomTypeEntity> list = classroomtypeService.selectClassTypeByBuilding(bid);
        return list;
    }
    //根据选择的楼栋id查询有此楼下有多少教室类型
    @RequestMapping("selectClassByClassType")
    @ResponseBody
    public  List<ClassroomEntity> selectClassByClassType(@RequestParam( "ctid") int ctid,HttpServletRequest request, Model model) {
        List<ClassroomEntity> list1 = classroomService.selectClassByClassType(ctid);
        return list1;
    }
    //添加教室租赁的方法
    @RequestMapping("addClassroomrental")
    @ResponseBody
    public String  addClassroomrental(HttpSession session,ClassroomRentalEntity classroomRental)
    {
        LeaseTimeEntity leaseTimeEntity = leasetimeService.queryLeasetimeById(classroomRental.getLtid().getLtid());
        Integer ltday1 = leaseTimeEntity.getLtday();
        classroomRental.setCstarttime(new Date().toLocaleString());
        Date date = MyDateUtils.addDay(new Date(), ltday1);
        classroomRental.setCendtime(date.toLocaleString());
        int i = classroomrentalService.insertClassroomrental(classroomRental);
        if (i>0){
            return "1";
        }else {
            return "0";
        }
    }
    @RequestMapping("CalculateRent")
    @ResponseBody
    public Double CalculateRent(ClassroomRentalEntity classroomRentalEntity){
        ClassroomEntity classroomEntity = classroomService.queryClassroomById(classroomRentalEntity.getCid().getCid());
        //教室押金
        double deposit = classroomEntity.getDeposit();
        //教室租金
        double rent = classroomEntity.getRent();
        LeaseTimeEntity leaseTimeEntity = leasetimeService.queryLeasetimeById(classroomRentalEntity.getLtid().getLtid());
        //租赁时长
        Integer ltday = leaseTimeEntity.getLtday();
        //折扣
        double ltcount = leaseTimeEntity.getLtcount();
        double total= deposit+rent*ltday*ltcount;
        return total;
    }
    @RequestMapping("checkedUserMoney")
    @ResponseBody
    public String checkedUserMoney(HttpSession session,double crmoney,ClassroomRentalEntity classroomRentalEntity){
        UserEntity user=(UserEntity)session.getAttribute("userEntity");
        //登录用户的账户余额
        double ubalance = user.getUbalance();
        if (ubalance>crmoney){
            double newubalance=ubalance-crmoney;
            user.setUbalance(newubalance);
            userService.userUpdateService(user);
            return "1";
        }if (ubalance<crmoney){
            return "0";
        }
        return "";
    }

    @RequestMapping("adminqueryClassroomrental")
    public String adminqueryClassroomrental(Model model){
        model.addAttribute("adminClassroomrental",classroomrentalService.adminqueryClassroomrental());
        return "adminclassroomrental-list";
    }

    @RequestMapping("UpdataClassroomrental")
    @ResponseBody
    public int UpdataClassroomrental(ClassroomRentalEntity classroomRentalEntity,HttpSession session){
        //修改租赁订单状态：1：审批通过
        classroomRentalEntity.setCrflag(1);
        int i = classroomrentalService.updateClassroomrental(classroomRentalEntity);
        //修改用户状态 1：租赁中
        ClassroomRentalEntity classroomRentalEntity2 = classroomrentalService.queryClassroomrentalById(classroomRentalEntity.getCrid());
        UserEntity userEntity2 = userService.queryUserById(classroomRentalEntity2.getUid().getUid());
        userEntity2.setUflag(1);
        int i1 = userService.userUpdateService(userEntity2);
        UserEntity userEntity1 = userService.queryUserById(userEntity2.getUid());
        session.setAttribute("userentity",userEntity1);
        //修改教室状态 1:租赁中
        ClassroomRentalEntity classroomRentalEntity1 = classroomrentalService.queryClassroomrentalById(classroomRentalEntity.getCrid());
        ClassroomEntity classroomEntity = classroomService.queryClassroomById(classroomRentalEntity1.getCid().getCid());
        classroomEntity.setCflag(1);
        int i2 = classroomService.updateClassroom(classroomEntity);
        if (i+i1+i2==3){
            return 1;
        }else{
            return 0;
        }

    }
    @RequestMapping("userUpdataClassroomrental1")
    @ResponseBody
    public int userUpdataClassroomrental(ClassroomRentalEntity classroomRentalEntity, HttpServletRequest request) {
        //修改租赁订单状态：2：归还待审批
        classroomRentalEntity.setCrflag(2);
        int i = classroomrentalService.updateClassroomrental(classroomRentalEntity);
        if (i>0){
            return 1;
        }else{
            return 0;
        }
    }

    @RequestMapping("adminqueryhuanClassroomrental")
    public String adminqueryhuanClassroomrental(Model model){
        model.addAttribute("adminClassroomrental",classroomrentalService.adminqueryhuanClassroomrental());
        return "adminclassroomrental-huanlist";
    }
    @RequestMapping("tuiyajin")
    @ResponseBody
    public int tuiyajin(ClassroomRentalEntity classroomRentalEntity ,Double monoey,HttpSession session){
        //修改用户余额及状态
        //获取当前用户余额
        ClassroomRentalEntity classroomRentalEntity2 = classroomrentalService.queryClassroomrentalById(classroomRentalEntity.getCrid());
        UserEntity userEntity1 = userService.queryUserById(classroomRentalEntity2.getUid().getUid());
        double ubalance = userEntity1.getUbalance();
        double money=ubalance+monoey;
        userEntity1.setUbalance(money);
        userEntity1.setUflag(0);
        int i1 = userService.userUpdateService(userEntity1);
        UserEntity userEntity2 = userService.queryUserById(userEntity1.getUid());
        session.setAttribute("userEntity",userEntity2);
        //修改教室状态
        ClassroomEntity classroomEntity = classroomService.queryClassroomById(classroomRentalEntity2.getCid().getCid());
        classroomEntity.setCflag(0);
        int i = classroomService.updateClassroom(classroomEntity);
        //修改订单状态
        ClassroomRentalEntity classroomRentalEntity1 = classroomrentalService.queryClassroomrentalById(classroomRentalEntity.getCrid());
        classroomRentalEntity1.setCrflag(3);
        int i2 = classroomrentalService.updateClassroomrental(classroomRentalEntity1);
        if (i+i1+i2==3){
            return 1;
        }else{
            return 0;
        }
    }

}